import { GraphicsLayer, GeoJSONLayer } from 'lark-map'
import SymbolUtils from './SymbolUtils';
// import pointsJson from "../../../datas/points.json"

/**
 * 图层管理工具
 */
const LayerUtils = {
  areaLayer: null,//行政区高亮图层
  shpFileLayer: null,//加载 shpFile 数据渲染图层

  /**
   * 设置地图服务是否可见
   * @param {*} services 
   * @param {*} view 
   */
  setMapServiceLayerVisible(services, view) {
    if (services && services.length > 0) {
      services.forEach(item => {
        let layer = view.map.findLayerById(item["id"]);
        if (layer) {
          layer.visible = item["visible"];
        }
      });
    }
  },
  /**
   * 创建渲染图层
   * @param {*} view 
   */
  createRenderDrawLayer(view){
    let areaLayer = new GraphicsLayer({
      id: 'renderDrawLayer'
    })
    view.map.add(areaLayer);
    return areaLayer;
  },
  /**
   * 获取行政区划图
   * @param {*} view 
   */
  getRenderDrawLayer(view) {
    let layer = view.map.findLayerById("renderDrawLayer");
    if (layer) return layer;
    return this.createRenderDrawLayer(view);
  },

  /**
   * 清除渲染数据
   */
  clearRenderDrawLayer(view){
    let layer = view.map.findLayerById("renderDrawLayer");
    if (layer){
      layer.removeAll();
    }
  },
  /**
   * 获取行政区临时图层
   * @param {*} view
   */
  createAreaLayer(view) {
    let areaLayer = new GraphicsLayer({
      id: 'areaLayer'
    })
    view.map.add(areaLayer);
    return areaLayer;
  },

  /**
   * 获取行政区划图
   * @param {*} view 
   */
  getAreaLayer(view) {
    let layer = view.map.findLayerById("areaLayer");
    if (layer) return layer;
    return this.createAreaLayer(view);
  },

  /**
   * 创建geojsonlayer
   * @param {*} view 
   */
  createGeoJsonLayer(view){
    let layer = new GeoJSONLayer({
      id: 'geoJsonDataLayer',
      renderer: SymbolUtils.DRAW_POLYGON_RENDER,
      spatialReference: view.spatialReference
    });
    view.map.add(layer);
    return layer;
  },


  
  /**
   * 加载 shpFile 数据渲染图层
   * @param {*} view
   */
  getGeoJsonLayer(view) {
    let layer = view.map.findLayerById("geoJsonDataLayer");
    if (layer) return layer;
    return this.createGeoJsonLayer(view);
  },

  /**
   * 清除GeoJsonLayer数据
   * @param {*} view
   */
  clearGeoJsonLayer(view) {
    let layer = view.map.findLayerById("geoJsonDataLayer");
    if (layer){
      layer.removeAll();
    }
  }
}
export default LayerUtils
